<template>
	<view>
		<!-- #ifdef H5 -->
		<view class="navbar" style="position: fixed;z-index: 100;text-align: center;width: 100%;top: 80upx;">
			<!-- #endif -->
			<!-- #ifndef H5 -->
			<view class="navbar" style="position: fixed;z-index: 100;text-align: center;width: 100%;top: 0;">
				<!-- #endif -->
				<view v-for="(item, index) in tabList" :key="index" class="nav-item" :class="{current: fromTabIndex === index}"
				 @tap="fromTabSelect(index)">
					{{item.name}}
				</view>
			</view>
			<view style="font-size: 24upx;color: #999999;margin: 100upx 16upx 0upx 16upx">
				提示: 收藏最多保存100个宝贝，如果超出100个宝贝，如果此时收藏了新的宝贝，最末尾的宝贝将会自动删除
			</view>
			<view class="index-content" style="background: transparent;">
				<view class="index-coupon has-pd-10 top_30">
					<view class="goods-list" v-if="couponlist.length > 0&&fromTabIndex==0">
						<orange-goods-card-home v-for="(item,index) in couponlist" :index="index%2" :tkmoney='item.tkmoney' :tkmoneys='item.tkmoneys'
						 :itemid="item.itemid" :isEnable='isEnable' :logo="logo" :is-invitation="isInvitation" :itempic="item.itempic+'_310x310.jpg'"
						 :itemtitle="item.itemtitle" :shopname='item.shopname' :itemprice='"¥"+item.itemprice' :itemsale='item.itemsale' :itemendprice='""+item.itemendprice'
						 :couponmoney="item.couponmoney"></orange-goods-card-home>
					</view>
					<view class="goods-list" v-if="couponlist.length > 0&&fromTabIndex==1">
						<orange-goods-card-home v-for="(item,index) in couponlist" :index="index%2" :logo="pddlogo" :tkmoney='item.tkmoney'
						 :tkmoneys='item.tkmoneys' :shopname='item.mallName' :itemid="item.goodsId" :isEnable='isEnable' :is-invitation="isInvitation" :itempic="item.goodsImageUrl"
						 :itemtitle="item.goodsName" :itemprice='"¥"+item.itemprice' :itemsale='"已售 "+item.salesTip' :itemendprice='item.itemendprice'
						 :couponmoney="item.couponDiscount*0.01">
						</orange-goods-card-home>
					</view>
					<view class="goods-list" v-if="couponlist.length > 0&&fromTabIndex==2">
						<orange-goods-card-home v-for="(item,index) in couponlist" :index='index' :tkmoney='item.tkmoney' :tkmoneys='item.tkmoneys'
						 :itemid="item.skuId" :logo="jdlogo" :isEnable='isEnable' :is-invitation="isInvitation" :itempic="item.itempic"
						 :itemtitle="item.skuName" :shopname='item.shopInfo.shopName' :itemprice='"¥"+item.itemprice' :itemsale='item.itemsale' :itemendprice='""+item.itemendprice'
						 :couponmoney="item.couponmoney" @success="clickItem"></orange-goods-card-home>
					</view>
					<view class="goods-list" style="display: flex;flex-wrap: wrap;justify-content: space-between;" v-if="couponlist.length > 0&&fromTabIndex==3">
						<view @tap='goDetail(item.id)' style="width: 49%;box-shadow: 1px 0 1px 1px rgb(0 0 0 / 10%);border-radius: 20upx;background-color: #fff;margin-top: 16upx;padding-bottom: 18upx;" v-for="(item,index) in couponlist" :index='index'>
							<view style="height: 400upx;"><image style="height: 400upx;border-radius: 8upx;" :src="item.coverImg"></image></view>
							<view style="margin-top: 8upx;">
								<view style="padding:0 4upx;height: 80rpx;font-size: 28rpx;margin-right: 8rpx;margin-left: 8rpx;overflow: hidden;margin-top: 8upx;">
									<text style="color: rgb(255, 255, 255);background: rgb(225, 10, 7);font-size: 20upx;padding: 4upx 12upx;border-radius: 12upx;margin-right: 8upx;">自营</text>
									<text>{{item.title}}</text>
								</view>
								<view style="padding:0 12upx;display: flex;flex-direction: column;justify-content: space-between;">
									<view style="padding: 10upx 0;">
										<view style="display: flex;">
											<view style="font-size: 28upx;color: #e10a07;font-weight: 700;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;">
												<text style="font-size:28upx;">¥</text>{{item.price}}</view>
											<view style="margin-left:20px;font-size: 24upx;color: #999999;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;text-align: left;">{{item.sales}}件</view>
										</view>
									</view>
									<view style="display: flex;align-items: center;font-size: 24upx;color: rgb(153, 153, 153);">
										<image style="height: 30upx;width:30upx;margin-right: 4upx;" src="../../static/shop.png"></image>
										<view>优集聚自营店铺</view>
									</view>
								</view>
							</view>
						</view>
					</view>
				</view>
				<!-- 悬浮上拉 -->
				<view class="scroll_top" @tap="topScrollTap" v-bind:class="[scrollTop ? 'active' : '','']" style="bottom: 56px;">
					<text class="iconfont icon-shangla"></text>
				</view>
				<!-- 空白页 -->
				<empty v-if="couponlist.length === 0" des="你还没有收藏，快去逛逛吧"></empty>
			</view>
		</view>
</template>

<script>
	export default {
		onShareAppMessage(res) {
			return {
				title: '购物先领券，一年省一半',
				path: '/pages/index/index'
			}
		},
		data() {
			return {
				tabList: [{
					position: 'tb',
					name: '淘宝'
				}, {
					position: 'pdd',
					name: '拼多多'
				}, {
					position: 'jd',
					name: '京东'
				}
				],
				jdlogo: "../../static/img/jd.png",
				logo: "../../static/img/mao.png",
				taobao: "../../static/img/taobao.png",
				pddlogo: "../../static/img/pinduoduo.png",
				zylogo: "../../static/img/zy.png",
				fromTabIndex: 0,
				couponlist: [],
				loadingType: 0,
				isInvitation: 0,
				isEnable: '否',
				scrollTop: false,
				collectKey: 'orange-sqx-collect',
				collectPddKey: 'orange-sqx-collect-pdd',
				collectJdKey: 'orange-sqx-collect-jd',
				collectZyKey: 'orange-sqx-collect-zy',

			}
		},
		onLoad: function(e) {
			let a = this.$queue.getData("isEnable")
			if (a) {
				this.isEnable = a;
			}
			this.getUserInfo();
			this.getTaoBao();
		},
		methods: {
			goDetail(id) {
				uni.navigateTo({
					url: '/pages/zysc/index/commoditydetail?ordersId=' + id
				});
			},
			clickItem(index) {
				this.$queue.setData("jd_list", JSON.stringify(this.couponlist[index]))
				uni.navigateTo({
					url: '/pages/detail/jd'
				});
			},
			getTaoBao() {
				this.$Request.getT('/collect/collectList', {
					type: 1,
					cate: "collect",
					uid: this.$queue.getData("userId")
				}).then(res => {
					let list = []
					for (var i = 0; i < res.data.length; i++) {
						list.push(JSON.parse(res.data[i]))
					}
					// console.log("1111",list)
					// this.$Request.getT("/common/getMemoByCode",{code:"tbkyhyjbl"}).then(res=>{
					// 	console.log("333",res.data)
					// 	this.grade=res.data
					// })
					list.forEach(d => {
						let grade = this.$queue.getData("grade");
						d.tkmoneys = (d.tkmoney * parseFloat(this.$queue.maxMoney())).toFixed(2);
						if (grade) {
							d.tkmoney = (d.tkmoney * parseFloat(grade)).toFixed(2);
						} else {
							d.tkmoney = (d.tkmoney * parseFloat(this.$queue.minMoney())).toFixed(2);
						}   
						d.itemsale = d.itemsale > 10000 ? "已售" + (d.itemsale / 10000).toFixed(1) + "万" : "已售" + d.itemsale;
						this.couponlist.push(d);
					});    
				}).catch(err => {
					console.log("cuowu", err)
				})
				// this.$queue.get(this.collectKey).forEach(d => {
				// 	let grade = this.$queue.getData("grade");
				// 	d.tkmoneys = (d.tkmoney * parseFloat(this.$queue.maxMoney())).toFixed(2);
				// 	if (grade) {
				// 		d.tkmoney = (d.tkmoney * parseFloat(grade)).toFixed(2);
				// 	} else {
				// 		d.tkmoney = (d.tkmoney * parseFloat(this.$queue.minMoney())).toFixed(2);
				// 	}
				// 	d.itemsale = d.itemsale > 10000 ? "已售" + (d.itemsale / 10000).toFixed(1) + "万" : "已售" + d.itemsale;
				// 	this.couponlist.push(d);
				// });
			},
			getPddd() {
				this.$Request.getT('/collect/collectList', {
					type: 2,
					cate: "collect",
					uid: this.$queue.getData("userId")
				}).then(res => {
					let list = []
					for (var i = 0; i < res.data.length; i++) {
						list.push(JSON.parse(res.data[i]))
					}
					console.log("1111",list)
					this.$Request.getT("/common/getMemoByCode",{code:"tbkyhyjbl"}).then(res=>{
						console.log("333",res.data)
						this.grade=res.data
					})
					list.forEach(d => {
						let grade = this.grade;
						d.tkmoneys = ((((d.minGroupPrice - d.couponDiscount) * 0.01) * (d.promotionRate * 0.001)) * parseFloat(this.$queue
							.maxMoney())).toFixed(2);
						if (grade) {
							d.tkmoney = ((((d.minGroupPrice - d.couponDiscount) * 0.01) * (d.promotionRate * 0.001)) * parseFloat(
								grade)).toFixed(2);
						} else {
							d.tkmoney = ((((d.minGroupPrice - d.couponDiscount) * 0.01) * (d.promotionRate * 0.001)) * parseFloat(this.$queue
								.minMoney())).toFixed(2);
						}
						d.itemendprice = ((d.minGroupPrice - d.couponDiscount) * 0.01).toFixed(2);
						d.itemprice = ((d.minGroupPrice) * 0.01).toFixed(2);
						this.couponlist.push(d);
					});
				
				}).catch(err => {
					console.log("cuowu", err)
				})
				
				// this.$queue.get(this.collectPddKey).forEach(d => {
				// 	let grade = this.$queue.getData("grade");
				// 	d.tkmoneys = ((((d.minGroupPrice - d.couponDiscount) * 0.01) * (d.promotionRate * 0.001)) * parseFloat(this.$queue.maxMoney())).toFixed(2);
				// 	if (grade) {
				// 		d.tkmoney = ((((d.minGroupPrice - d.couponDiscount) * 0.01) * (d.promotionRate * 0.001)) * parseFloat(
				// 			grade)).toFixed(2);
				// 	} else {
				// 		d.tkmoney = ((((d.minGroupPrice - d.couponDiscount) * 0.01) * (d.promotionRate * 0.001)) * parseFloat(this.$queue.minMoney())).toFixed(2);
				// 	}
				// 	d.itemendprice = ((d.minGroupPrice - d.couponDiscount) * 0.01).toFixed(2);
				// 	d.itemprice = ((d.minGroupPrice) * 0.01).toFixed(2);
				// 	this.couponlist.push(d);
				// });
			},
			getJD() {
				this.$Request.getT('/collect/collectList', {
					type: 3,
					cate: "collect",
					uid: this.$queue.getData("userId")
				}).then(res => {
					let list = []
					for (var i = 0; i < res.data.length; i++) {
						list.push(JSON.parse(res.data[i]))
					}
					console.log("1111",list)
					this.$Request.getT("/common/getMemoByCode",{code:"tbkyhyjbl"}).then(res=>{
						console.log("333",res.data)
						this.grade=res.data
					})
					list.forEach(d => {
						let grade = this.grade;
						d.itemsale = d.inOrderCount30Days > 10000 ? "已售" + (d.inOrderCount30Days / 10000).toFixed(1) + "万" : "已售" + d.inOrderCount30Days;
						if (d.commissionInfo.couponCommission) {
							this.couponlist.push(d);
						}
					});
				
				}).catch(err => {
					console.log("cuowu", err)
				})
				// this.$queue.get(this.collectJdKey).forEach(d => {
				// 	let grade = this.$queue.getData("grade");
				// 	d.itemsale = d.inOrderCount30Days > 10000 ? "已售" + (d.inOrderCount30Days / 10000).toFixed(1) + "万" : "已售" + d.inOrderCount30Days;
					
				// 	if(d.commissionInfo.couponCommission){
				// 		this.couponlist.push(d);
				// 	}
				// });
			},
			getZy() {
				this.$queue.get(this.collectZyKey).forEach(d => {
					let grade = this.$queue.getData("grade");
					d.descrition = '';
					if (d.commissionPrice != 0) {
						if (grade) {
							if (this.relation) {
								d.descrition = ((parseFloat(d.memberPrice) * parseFloat(d.commissionPrice)) * parseFloat(grade)).toFixed(
									2);
							} else {
								d.descrition = ((parseFloat(d.price) * parseFloat(d.commissionPrice)) * parseFloat(grade)).toFixed(2);
							}
						} else {
							if (this.relation) {
								d.descrition = ((parseFloat(d.memberPrice) * parseFloat(d.commissionPrice)) * parseFloat(this.$queue.minMoney()))
									.toFixed(2);
							} else {
								d.descrition = ((parseFloat(d.price) * parseFloat(d.commissionPrice)) * parseFloat(this.$queue.minMoney()))
									.toFixed(2);
							}
						}
					}
					d.sales = d.sales > 10000 ? "已售" + (d.sales / 10000).toFixed(1) + '万' : d.sales;
					this.couponlist.push(d);
					console.log(this.couponlist)
				});
			},
			fromTabSelect(index) {
				this.fromTabIndex = index;
				this.couponlist = [];
				if (index == 0) {
					this.getTaoBao();
				}
				if (index == 1) {
					this.getPddd();
				}
				if (index == 2) {
					this.getJD();
				}
				if (index == 3){
					this.getZy();
				}
			},
			getUserInfo() {
				let userId = this.$queue.getData("userId");
				if (userId) {
					this.$Request.getT("/user/" + userId).then(res => {
						if (res.status === 0) {
							this.$queue.setData("image_url", res.data.image_url);
							this.$queue.setData("mobile", res.data.phone);
							this.isInvitation = res.data.isInvitation;
							this.$queue.setData("isInvitation", res.data.isInvitation);
							this.$queue.setData("relation", res.data.invitation);
							this.$queue.setData("grade", res.data.grade);
							this.$queue.setData("nickName", res.data.nickName);
							this.$queue.setData("relation_id", res.data.relationId);
							this.$queue.setData("gender", parseInt(res.data.gender));
						}
					});
				}
			},
			topScrollTap: function() {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 300
				});
			},
			onPageScroll: function(e) {
				this.scrollTop = e.scrollTop > 200;
			}
		},

	}
</script>

<style lang="scss">
	@import "../../static/css/index.css";

	.navbar {
		display: flex;
		height: 80upx;
		padding: 0 5px;
		background: #fff;
		box-shadow: 0 2upx 10upx rgba(0, 0, 0, .06);
		position: relative;
		z-index: 10;

		.nav-item {
			flex: 1;
			display: flex;
			justify-content: center;
			align-items: center;
			height: 100%;
			font-size: 32upx;
			color: $font-color-dark;
			position: relative;

			&.current {
				color: $base-color;

				&:after {
					content: '';
					position: absolute;
					left: 50%;
					bottom: 0;
					transform: translateX(-50%);
					width: 44px;
					height: 0;
					border-bottom: 4upx solid $base-color;
				}
			}
		}
	}
</style>
